package com.start_eternity.oasissafewalk.controller;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.pojo.*;
import com.start_eternity.oasissafewalk.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    public StudentService studentService;

    /*根据id删除*/
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable List<Integer> id){
        log.info("根据id删除：{}",id);
        int delete = studentService.delete(id);
        if (delete > 0) {
            return Result.success();
        }
        return Result.error("根据id删除失败");
    }


    /*添加*/
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("添加学生：{}",student);
        int save = studentService.save(student);
        if (save > 0) {
            return Result.success();
        }
        return Result.error("学生添加失败");
    }

    /*根据id查询信息*/
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询信息：{}",id);
        List<ParentStudent> parentStudents = studentService.selectById(id);
        if (!parentStudents.isEmpty()) {
            return Result.success(parentStudents);
        }
        return Result.error("根据id查询学生信息失败");
    }


    /*修改信息*/
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改信息：{}",student);
        int update = studentService.update(student);
        if (update > 0) {
            return Result.success();
        }
        return Result.error("未成功修改信息");
    }

    //分页查询
    @GetMapping("/page")
    public Result queryPage(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10")Integer pageSize) {
        PageBean pageBean = studentService.page(page, pageSize);

        if (pageBean != null) {
            return Result.success(pageBean);
        }
        return Result.error("未查询到相关信息");
    }

}
